/**
 * 
 */

package com.shy.portal.controller;

import java.util.Date;
import java.util.LinkedHashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.shy.base.controller.AbsController;
import com.shy.base.util.BeanUtil;
import com.shy.base.vo.AppConstants;
import com.shy.base.vo.EasyuiPagerVo;
import com.shy.base.vo.PagerFilter;
import com.shy.portal.entity.Permission;
import com.shy.portal.service.MenuService;
import com.shy.portal.service.PermissionService;

/**
 * <p>
 * Title:PermissionController
 * </p>
 * <p>
 * Description:权限管理控制器
 * </p>
 * <p>
 * Company:ipebg
 * </p>
 * 
 * @author H2013788
 */
@Controller
@RequestMapping("/permission")
public class PermissionController extends AbsController {

	/**
     * log
     */
	private Logger logger = LogManager.getLogger(this.getClass());
    
    /**
     * 权限service层
     */
    @Resource
    private PermissionService permissionService;
    
    /**
     * 菜单service层
     */
    @Resource
    private MenuService menuService;

    /**
     * 请求菜单列表
     * @param model 参数模型
     * @return 权限列表
     * @author H2201965
     * @date 2018/2/6下午2:44:57
     */
    @RequestMapping("/list_page")
    public String showPermissionListPage(Model model) {
        return "portal/permission_list";
    }

    /**
     * 異步加载菜单列表数据
     * 
     * @param permission
     *            查询條件
     * @param page 頁
     * @param rows 行
     * @author H2013788
     * @date 2017/10/6下午1:55:58
     */
    @RequestMapping("/list_data")
    public void loadPermissionList(Permission permission, Integer page, Integer rows, HttpServletResponse response) {
        PagerFilter pagerFilter = getPageable(rows == null ? 10L : rows);
        pagerFilter.setPagePer(null == page ? 1 : page);
        if (null != permission.getMenu() && null == permission.getMenu().getId()) {
            permission.setMenu(null);
        }
        LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
        orderBy.put("menu.id", PagerFilter.DIRECTION_ASC);
        orderBy.put("id", PagerFilter.DIRECTION_ASC);
        pagerFilter.setOrderby(orderBy);
        pagerFilter = permissionService.listByPager(permission, null, pagerFilter);
        writeResponse(new StringBuffer(gsonDateTimeExpose.toJson(new EasyuiPagerVo(pagerFilter))), response);
    }

    /**
     * 菜单預编辑，按照ID查询菜单信息
     * 
     * @param id 权限ID
     * @author H2013788
     * @date 2017/10/6下午1:57:19
     */
    @RequestMapping("/edit_permission")
    public void editPermission(Integer id, HttpServletResponse response) {
        if (null != id) {
            Permission permission = permissionService.getById(id);
            writeResponse(new StringBuffer(gsonDateTimeExpose.toJson(permission)), response);
        } else {
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.GET_OBJECT_NULL), response);
        }
    }

    /**
     * 保存菜单信息，用於新增或编辑菜单
     * 
     * @param permission 权限實体
     * @author H2013788
     * @date 2017/10/6下午1:58:54
     */
    @RequestMapping("/save_permission")
    public void savePermission(Permission permission, HttpServletResponse response) {
        if (null == permission) {
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.REQUEST_PARAM_NULL), response);
        } else {
            try {
                if (null != permission.getId()) {
                    Permission old = permissionService.getById(permission.getId());
                    BeanUtil.merge(old, permission);
                    permission.setUpdateTime(new Date());
                    permission.setUpdateUser(getShiroUser().getLoginName());
                    permissionService.update(old);
                } else {
                    permission.setCreateTime(new Date());
                    permission.setCreateUser(getShiroUser().getLoginName());
                    permissionService.save(permission);
                }
                respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.SAVE_SUCCESS), response);
            } catch (Exception e) {
                e.printStackTrace();
                respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.SAVE_ERROR), response);
                logger.error(AppConstants.configMap.get(AppConstants.SAVE_ERROR) + e);
            }
        }

    }

    /**
     * 刪除指定菜单
     * 
     * @param id 菜单ID
     * @author H2013788
     * @date 2017/10/6下午1:59:18
     */
    @RequestMapping("delete_permission")
    public void deletePermission(Integer id, HttpServletResponse response) {
        try {
            if (null != id) {
                permissionService.deleteById(id);
                respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.OPERATE_SUCCESS), response);
            } else {
                respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.REQUEST_PARAM_NULL), response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.OPERATE_ERROR), response);
            logger.error(AppConstants.configMap.get(AppConstants.DELETE_ERROR) + e);
        }
    }

}
